﻿@model Nop.Plugin.Shipping.FixedByWeightByTotal.Models.ConfigurationModel
@inject Nop.Core.Domain.Common.AdminAreaSettings adminAreaSettings
@inject Nop.Services.Stores.IStoreService storeService
@{
    var stores = storeService.GetAllStores();
}
<script type="text/javascript">
    $(document).ready(function () {
        $("#@Html.IdFor(model => model.SearchCountryId)").change(function () {
            var selectedItem = $(this).val();
            var ddlStates = $("#@Html.IdFor(model => model.SearchStateProvinceId)");
            $.ajax({
                cache: false,
                type: "GET",
                url: "@(Url.Action("GetStatesByCountryId", "Country"))",
                data: { "countryId": selectedItem, "addAsterisk": "true" },
                success: function (data) {
                    ddlStates.html('');
                    $.each(data, function (id, option) {
                        ddlStates.append($('<option></option>').val(option.id).html(option.name));
                    });
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert('Failed to retrieve states.');
                }
            });
        });
    });
</script>

<form asp-controller="FixedByWeightByTotal" asp-action="Configure" method="post" id="shipping-byweight-form">
    <div class="content advanced-setting">
        <div class="form-horizontal">
            <div class="panel-group">
                <div class="panel panel-default panel-search">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-5">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchStoreId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchStoreId" asp-items="Model.AvailableStores" />
                                    </div>
                                </div>
                                <div class="form-group" )>
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchWarehouseId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchWarehouseId" asp-items="Model.AvailableWarehouses" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchShippingMethodId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchShippingMethodId" asp-items="Model.AvailableShippingMethods" />
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-7">
                                <div class="form-group" )>
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchCountryId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchCountryId" asp-items="Model.AvailableCountries" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchStateProvinceId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchStateProvinceId" asp-items="Model.AvailableStates" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchZip" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="SearchZip" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-7 col-md-offset-5">
                                <button type="button" id="search-shipping-byweight-records" class="btn btn-primary btn-search">
                                    <i class="fa fa-search"></i>
                                    @T("Admin.Common.Search")
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel panel-default">
                    <div class="panel-body">
                        <p>
                            <b>@T("Plugins.Shipping.FixedByWeightByTotal.Formula")</b>
                            @T("Plugins.Shipping.FixedByWeightByTotal.Formula.Value")
                        </p>
                        <div id="shipping-byweight-grid"></div>
                        <script>
                            $(document)
                                .ready(function() {
                                    $("#shipping-byweight-grid")
                                        .kendoGrid({
                                            dataSource: {
                                                type: "json",
                                                transport: {
                                                    read: {
                                                        url:
                                                            "@Html.Raw(Url.Action("RateByWeightByTotalList", "FixedByWeightByTotal"))",
                                                        type: "POST",
                                                        dataType: "json",
                                                        data: additionalData
                                                    },
                                                    destroy: {
                                                        url:
                                                            "@Html.Raw(Url.Action("DeleteRateByWeightByTotal", "FixedByWeightByTotal"))",
                                                        type: "POST",
                                                        dataType: "json",
                                                        data: addAntiForgeryToken
                                                    }
                                                },
                                                schema: {
                                                    data: "Data",
                                                    total: "Total",
                                                    errors: "Errors",
                                                    model: {
                                                        id: "Id"
                                                    }
                                                },
                                                requestEnd: function(e) {
                                                    if (e.type == "update") {
                                                        this.read();
                                                    }
                                                },
                                                error: function(e) {
                                                    display_kendoui_grid_error(e);
                                                    // Cancel the changes
                                                    this.cancelChanges();
                                                },
                                                pageSize: @(adminAreaSettings.DefaultGridPageSize),
                                                serverPaging: true,
                                                serverFiltering: true,
                                                serverSorting: true
                                            },
                                            pageable: {
                                                refresh: true,
                                                pageSizes: [@(adminAreaSettings.GridPageSizes)],
                                                @await Html.PartialAsync("~/Areas/Admin/Views/Shared/_GridPagerMessages.cshtml")
                                            },
                                            editable: {
                                                confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                                mode: "inline"
                                            },
                                            scrollable: false,
                                            columns: [
                                                {
                                                    field: "StoreName",
                                                    hidden: @(stores.Count > 1 ? "false" : "true"),
                                                    title: "@T("Plugins.Shipping.FixedByWeightByTotal.Fields.Store")",
                                                    width: 200
                                                }, {
                                                    field: "WarehouseName",
                                                    title:
                                                        "@T("Plugins.Shipping.FixedByWeightByTotal.Fields.Warehouse")",
                                                    width: 200
                                                }, {
                                                    field: "CountryName",
                                                    title: "@T("Plugins.Shipping.FixedByWeightByTotal.Fields.Country")",
                                                    width: 200
                                                }, {
                                                    field: "StateProvinceName",
                                                    title:
                                                        "@T("Plugins.Shipping.FixedByWeightByTotal.Fields.StateProvince")",
                                                    width: 200
                                                }, {
                                                    field: "Zip",
                                                    title: "@T("Plugins.Shipping.FixedByWeightByTotal.Fields.Zip")",
                                                    width: 200
                                                }, {
                                                    field: "ShippingMethodName",
                                                    title:
                                                        "@T("Plugins.Shipping.FixedByWeightByTotal.Fields.ShippingMethod")",
                                                    width: 200
                                                }, {
                                                    field: "DataHtml",
                                                    title: "@T("Plugins.Shipping.FixedByWeightByTotal.Fields.DataHtml")",
                                                    width: 400,
                                                    encoded: false
                                                }, {
                                                    field: "Id",
                                                    title: "@T("Admin.Common.Edit")",
                                                    width: 100,
                                                    headerAttributes: { style: "text-align:center" },
                                                    attributes: { style: "text-align:center" },
                                                    template:
                                                        "<button onclick=\"javascript:OpenWindow('@Url.Action("EditRateByWeightByTotalPopup", "FixedByWeightByTotal")?id=#=Id#&btnId=btnRefresh&formId=shipping-byweight-form', 800, 800, true); return false;\" class='btn btn-default'><i class=\"fa fa-pencil\"></i>@T("Admin.Common.Edit")</button>"
                                                }, {
                                                    command: { name: "destroy", text: "@T("Admin.Common.Delete")" },
                                                    title: "@T("Admin.Common.Delete")",
                                                    width: 100
                                                }
                                            ]
                                        });
                                });
                        </script>
                    </div>
                    <div class="panel-footer">
                        <button type="submit" id="btnAddNewRecord" class="btn btn-primary"
                                onclick="javascript:OpenWindow('@(Url.Action("AddRateByWeightByTotalPopup", "FixedByWeightByTotal", new { btnId = "btnRefresh", formId = "shipping-byweight-form" }))', 800, 800, true); return false;">
                            @T("Plugins.Shipping.FixedByWeightByTotal.AddRecord")
                        </button>
                        <input type="submit" id="btnRefresh" class="btn btn-default" style="display: none" />
                    </div>
                </div>
                <div class="panel panel-advanced-setting">
                    <div class="form-group">
                        <div class="col-md-3">
                            <nop-label asp-for="LimitMethodsToCreated" />
                        </div>
                        <div class="col-md-9">
                            <nop-editor asp-for="LimitMethodsToCreated" />
                            <span asp-validation-for="LimitMethodsToCreated"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            &nbsp;
                        </div>
                        <div class="col-md-9">
                            <input type="button" id="savegeneralsettings" class="btn btn-primary" value="@T("Admin.Common.Save")" />
                            <script>
                                $(document)
                                    .ready(function() {
                                        $('#savegeneralsettings').click(function() {
                                            var postData = $(this.form).serialize();
                                            addAntiForgeryToken(postData);

                                            $.ajax({
                                                cache: false,
                                                type: 'POST',
                                                url: '@Url.Action("Configure", "FixedByWeightByTotal")',
                                                data: postData,
                                                dataType: 'json',
                                                success: function(data) {
                                                    alert('Saved');
                                                },
                                                error: function(xhr, ajaxOptions, thrownError) {
                                                    alert('Error while saving.');
                                                }
                                            });
                                            return false;
                                        });
                                    });
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        $(document)
            .ready(function () {
                $('#btnRefresh')
                    .click(function () {
                        //refresh grid
                        var grid = $("#shipping-byweight-grid").data('kendoGrid');
                        //grid.dataSource.page(1); //new search. Set page size to 1
                        grid.dataSource.read();

                        //return false to don't reload a page
                        return false;
                    });
                $('#search-shipping-byweight-records').click(function () {
                    //search
                    var grid = $('#shipping-byweight-grid').data('kendoGrid');
                    grid.dataSource.page(1); //new search. Set page size to 1
                    return false;
                });
            });

        function additionalData() {
            var data = {
                SearchStoreId: $('#SearchStoreId').val(),
                SearchWarehouseId: $('#SearchWarehouseId').val(),
                SearchCountryId: $('#SearchCountryId').val(),
                SearchStateProvinceId: $('#SearchStateProvinceId').val(),
                SearchZip: $('#@Html.IdFor(model => model.SearchZip)').val(),
                SearchShippingMethodId: $('#SearchShippingMethodId').val()
            };
            addAntiForgeryToken(data);
            return data;
        }
    </script>

</form>